<template>
  <!-- 租金 -->
  <div>
    <el-input
      :placeholder="$t('请输入租金')"
      disabled
      @change="changeData"
      v-model="checkedData"
    ></el-input>
  </div>
</template>
<script>
import nonStandardService from '@/services/nonStandardService'
import eventBus from '@/assets/utils/eventBus.js'
export default {
  name: 'rentD',
  props: {
    detail: {
      type: Object,
      default: () => {
        return {}
      },
    },
    value: [Object, String],
    disabled: Boolean,
    parameter: {
      type: Object,
      default: () => {
        return {}
      },
    },
    data: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      checkedData: '',
      readonly: false,
      startTime: null,
      endTime: null,
      priceUnit: null,
      acreage: 0,
      rentUnitPrice: 0,
      monthDiff: 0,
      yearDiff: 0,
    }
  },
  created() {
    this.readonly = Boolean(this.$route.query.readonly)
    this.setData()
    // 计租面积
    eventBus.$on('acreageChange', (data) => {
      this.acreage = data
      this.calcRentD()
    })
    // 租金单价(含税)
    eventBus.$on('rentUnitPriceChange', (val) => {
      this.rentUnitPrice = val
      this.calcRentD()
    })
    // 单价单位
    eventBus.$on('priceUnitChange', (data) => {
      this.priceUnit = data
      this.calcRentD()
    })
    // 有效期
    let startEndTime = JSON.parse(localStorage.getItem('startEndTime'))
    if (startEndTime) {
      this.startTime = startEndTime[0]
      this.endTime = startEndTime[1]
      this.getTimeDiff()
      this.calcRentD()
    }
    // 租赁项目
    eventBus.$on('leaseChange', (data) => {
      if (data) {
        this.acreage = data.rentalArea || 0
        this.rentUnitPrice = data.referencePrice || 0
        this.priceUnit = data.priceUnitCode || 0
        this.acreage = data.rentalArea || 0
        this.calcRentD()
      }
    })
  },
  methods: {
    getTimeDiff() {
      this.monthDiff = 0
      this.yearDiff = 0
      if (this.startTime && this.endTime) {
        // 解析日期字符串为Date对象
        const startDate = new Date(this.startTime)
        const endDate = new Date(this.endTime)
        // 获取年份和月份的差异
        let yearDiff = endDate.getFullYear() - startDate.getFullYear()
        let monthDiff = endDate.getMonth() - startDate.getMonth()
        // 如果结束月份小于开始月份，则需要从年份差异中减去1年，并加上12个月（因为每个月有30天左右的差异）
        if (monthDiff < 0) {
          yearDiff--
          monthDiff += 12
        }
        // 根据结束期限日是否晚于开始期限日设置X的值
        let x = endDate.getDate() > startDate.getDate() ? 1 : 0
        this.monthDiff = yearDiff * 12 + monthDiff + x
        // “有效期限”包含的月个数/12 向上求整
        this.yearDiff = Math.ceil(this.monthDiff / 12)
        console.log(this.monthDiff, this.yearDiff, '*********')
      }
    },
    /**
     * 租金单价 rentUnitPrice，计租面积 acreage，有效期限包含几个月 monthDiff，有效期限包含几年 yearDiff
     * ①当“单价单位=JGDW4元/月”时，租金=租金单价(含税) * 有效期限包含几个月
     * ②当“单价单位=JGDW3元/年”时，租金=租金单价(含税) * 有效期限包含几年
     * ③当“单价单位=JGDW1元/㎡·月”时，租金=租金单价(含税) * 计租面积 * 有效期限包含几个月
     * ④当“单价单位=JGDW2元/㎡·年”时，租金=租金单价(含税) * 计租面积 * 有效期限包含几年
     * ⑤当“单价单位=JGDW5元”时，租金=租金单价(含税)
     */
    calcRentD() {
      if (this.priceUnit) {
        switch (this.priceUnit) {
          case 'JGDW4':
            this.checkedData = this.rentUnitPrice * this.monthDiff
            break
          case 'JGDW3':
            this.checkedData = this.rentUnitPrice * this.yearDiff
            break
          case 'JGDW1':
            this.checkedData =
              this.rentUnitPrice * this.acreage * this.monthDiff
            break
          case 'JGDW2':
            this.checkedData = this.rentUnitPrice * this.acreage * this.yearDiff
            break
          case 'JGDW5':
            this.checkedData = this.rentUnitPrice
            break
        }
        this.$emit('input', String(this.checkedData))
      }
    },
    changeData() {
      this.$emit('input', String(this.checkedData))
    },
    setData() {
      // 计租面积
      if (this.data.acreage) {
        this.acreage = JSON.parse(this.data.acreage).value
          ? JSON.parse(this.data.acreage).value
          : 0
      }
      // 租金单价(含税)
      if (this.data.rentUnitPrice) {
        this.rentUnitPrice = JSON.parse(this.data.rentUnitPrice).value
          ? JSON.parse(this.data.rentUnitPrice).value
          : 0
      }
      // 单价单位
      if (this.data.priceUnit) {
        this.priceUnit = JSON.parse(this.data.priceUnit).value
          ? JSON.parse(this.data.priceUnit).checkedData
          : ''
      }
      // 取有效期数据
      let baseArr = (this.detail.parameterClassifyList || []).filter(
        (it) => it.parameterClassifyNumber === 'BasicInformation'
      )
      let timeInfo = ((baseArr[0] && baseArr[0].headerList) || []).find(
        (it) => it.parameterNumber === 'startEndTime'
      ) || {}
      if (timeInfo.defaultValue) {
        this.startTime = timeInfo.defaultValue[0]
        this.endTime = timeInfo.defaultValue[1]
        this.getTimeDiff()
      }
      if (this.value) {
        this.checkedData =
          JSON.parse(this.value).value || JSON.parse(this.value).value == 0
            ? JSON.parse(this.value).value
            : ''
      }
    },
    getData() {
      return {
        value: this.checkedData,
      }
    },
  },
}
</script>
